import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sinosheets/components/NetWorkImage/NetworkImageWidget.dart';
import 'package:sinosheets/r.dart';
import 'package:sinosheets/route/route_utils.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

class RecommendedShopItem extends StatelessWidget {
  final Map<String, dynamic> item;
  final VoidCallback? onTap;

  const RecommendedShopItem({
    super.key,
    required this.item,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap ??
          () {
            // 默认点击行为：打开店铺链接
            _openShopUrl();
          },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16.r),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.06),
              blurRadius: 12.r,
              offset: Offset(0, 6.r),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 店铺Logo区域
            Expanded(
              flex: 4,
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(16.r),
                    topRight: Radius.circular(16.r),
                  ),
                  gradient: LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color(0xFFF8F9FA),
                      Color(0xFFF1F3F4),
                    ],
                  ),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(16.r),
                    topRight: Radius.circular(16.r),
                  ),
                  child: NetworkImageWidget(
                    url: item['shopLogo'] ?? '',
                    fit: BoxFit.fill,
                    placeholder: R.assetsImgPlaceholderSearch,
                  ),
                ),
              ),
            ),
            // 店铺信息区域
            Expanded(
              flex: 2,
              child: Padding(
                padding: EdgeInsets.all(12.r),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 店铺名称
                    Text(
                      item['shopName'] ?? '',
                      style: StyleUtils.ts_1f_13_600.copyWith(
                        color: Color(0xFF222244),
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    SizedBox(height: 8.r),
                    // 来源类型标签
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 8.r, vertical: 4.r),
                      decoration: BoxDecoration(
                        color: _getSourceTypeColor(item['sourceType'] ?? ''),
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                      child: Text(
                        _getSourceTypeText(item['sourceType'] ?? ''),
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 10.r,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 获取来源类型颜色
  Color _getSourceTypeColor(String sourceType) {
    switch (sourceType.toLowerCase()) {
      case 'wd':
        return Color(0xFF00C851); // 微店绿色
      case 'tb':
        return Color(0xFFFF6B35); // 淘宝橙色
      case 'tm':
        return Color(0xFFFF0000); // 天猫红色
      case 'jd':
        return Color(0xFFE1251B); // 京东红色
      default:
        return Color(0xFF666666); // 默认灰色
    }
  }

  /// 获取来源类型文本
  String _getSourceTypeText(String sourceType) {
    switch (sourceType.toLowerCase()) {
      case 'wd':
        return 'Weidian';
      case 'tb':
        return 'Taobao';
      case 'tm':
        return 'Tmall';
      case 'jd':
        return 'JD';
      default:
        return sourceType.toUpperCase();
    }
  }

  /// 打开店铺链接
  void _openShopUrl() {
    String shopUrl = item['shopUrl'] ?? '';

    ///跳转itaobuy店铺
    ///拼接规则：https://www.itaobuy.com/shop-detail?url=https%3A%2F%2Fweidian.com%2F%3Fuserid%3D1736445632
    if (shopUrl.isNotEmpty) {
      String url = 'https://www.itaobuy.com/shop-detail?url=${Uri.encodeComponent(shopUrl)}';
      RouteUtils.toWebView(url: url);
    }
  }
}
